//
//  SCPhotoBrowserModalAnimation.swift
//  GZWeibo007
//
//  Created by newuser on 16/1/2.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class SCPhotoBrowserModalAnimation: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //获取控制器
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! SCPhotoBrowserController
        
        // 获取到modal出来的控制器的view
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //添加到容器视图
        transitionContext.containerView()?.addSubview(toView)
        
        //将过度视图添加到容器
        let tempImageView = toVC.modalTempImageView()
        
        //将过渡视图添加
        transitionContext.containerView()?.addSubview(tempImageView)
        
        
        
        toView.alpha = 0
        
        toVC.collectionView.hidden = true
        // 弹出的动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            toView.alpha = 1
            
            //让过渡视图缩放到目标位置
            tempImageView.frame = toVC.modelTargetRect()
            
            }) { (_) -> Void in
                
                //移除过渡视图
                tempImageView.removeFromSuperview()
                
                //显示collectionView
                toVC.collectionView.hidden = false
                
                transitionContext.completeTransition(true)
        }
    }
}

